home *** CD-ROM | disk | FTP | other *** search
/ Workbench Design / WB Collection.iso / workbench werkzeuge / scherz programme / clicker / source / broker.c next >
C/C++ Source or Header  |  1996-04-07  |  3KB  |  136 lines

  1. /*  File:         broker.c
  2.  *  Created:      20-10-95
  3.  *  Updated:      30-12-95
  4.  *  Version:      1.0
  5.  *  Project:      Clicker
  6.  *  Owner:        Jeroen Vermeulen
  7.  *  Requirements: KickStart V39+
  8.  *  Legal:        PD
  9.  *  Status:       Release
  10.  */
  11.  
  12. #include <proto/exec.h>
  13. #include <exec/memory.h>
  14. #include <proto/commodities.h>
  15. #include <proto/alib.h>
  16.  
  17. #include "main.h"
  18. #include "broker.h"
  19.  
  20. static STRPTR
  21.         InstallFailCxBrk = "Couldn't set up commodities broker!\n",
  22.         AllocFailCxObj   = "Unable to create CX object!\n";
  23.  
  24.  
  25. /* MakeBroker():
  26.  * Creates and initializes a NewBroker structure, including its MsgPort.
  27.  * Call KillBroker() to get rid of it later. If allocation fails for
  28.  * some reason, NULL is returned and the pointer indicated by errptr is
  29.  * redirected to an error string.
  30.  */
  31. struct NewBroker *MakeBroker(STRPTR *const errptr, const BYTE cx_pri)
  32. {
  33.   struct NewBroker *mybroker=NULL;
  34.   struct MsgPort   *msgport;
  35.  
  36.   /* --- */
  37.  
  38.   if ((msgport = CreateMsgPort()))
  39.   {
  40.     mybroker = AllocMem(sizeof(struct NewBroker),MEMF_PUBLIC|MEMF_ANY);
  41.     if (mybroker)
  42.     {
  43.       mybroker->nb_Version = NB_VERSION;
  44.       mybroker->nb_Name = "Clicker";
  45.       mybroker->nb_Title = descrp;
  46.       mybroker->nb_Descr = "Keyclick";
  47.       mybroker->nb_Unique = NBU_UNIQUE | NBU_NOTIFY;
  48.       mybroker->nb_Flags = COF_SHOW_HIDE;
  49.       mybroker->nb_Pri = cx_pri;
  50.       mybroker->nb_Port = msgport;
  51.       mybroker->nb_ReservedChannel = 0;
  52.     }
  53.     else
  54.     {
  55.       *errptr = InstallFailCxBrk;
  56.       DeleteMsgPort(msgport);
  57.     }
  58.   }
  59.   else *errptr = AllocFailMsgPort;
  60.   return(mybroker);
  61. }
  62.  
  63.  
  64. /* KillBroker():
  65.  * Delete a NewBroker structure (and its MsgPort) that was allocated
  66.  * through MakeBroker(). Calling this for an unitialized or NULL broker
  67.  * is safe, as long as initialization was done by MakeBroker().
  68.  */
  69. void KillBroker(struct NewBroker *const brokerobj)
  70. {
  71.   if (brokerobj)
  72.   {
  73.     struct MsgPort *port;
  74.     /* --- */
  75.     if ((port = brokerobj->nb_Port))
  76.     {
  77.       struct Message *msg;
  78.       /* --- */
  79.       while ((msg = GetMsg(port))) ReplyMsg(msg);
  80.       DeleteMsgPort(port);
  81.     }
  82.     FreeMem(brokerobj,sizeof(struct NewBroker));
  83.   }
  84. }
  85.  
  86.  
  87. /* SetupBroker():
  88.  * Sets up the commodities broker network for Clicker (created with
  89.  * MakeBroker()) in an inactive state.  Activate or deactivate it with
  90.  * ActivateCxObj().
  91.  * If successful, a pointer to the root CxObj is returned, so it can later be
  92.  * deallocated with DeleteCxObjAll().  If an error occurs, NULL is returned and
  93.  * the STRPTR pointed to by errptr will point to an error message.
  94.  */
  95. CxObj *SetupBroker(STRPTR *const errptr, struct NewBroker *const mybroker)
  96. {
  97.   CxObj  *brokerobj;
  98.   LONG    cxreturn=CBERR_OK;
  99.  
  100.   /* --- */
  101.  
  102.   brokerobj = CxBroker(mybroker,&cxreturn);
  103.  
  104.   switch (cxreturn)
  105.   {
  106.     case CBERR_OK:
  107.       {
  108.         CxObj *const senderobj = CxSender(mybroker->nb_Port,0);
  109.         BOOL   okay = FALSE;
  110.         /* --- */
  111.         if (senderobj)
  112.         {
  113.           AttachCxObj(brokerobj,senderobj);
  114.           if (!(okay = !CxObjError(senderobj))) *errptr = AllocFailCxObj;
  115.         }
  116.         else *errptr = AllocFailCxObj;
  117.  
  118.         if (!okay)
  119.         {
  120.           DeleteCxObjAll(brokerobj);
  121.           brokerobj = NULL;
  122.         }
  123.       }
  124.     break;
  125.  
  126.     case CBERR_DUP:
  127.     break;
  128.  
  129.     default:
  130.       *errptr = InstallFailCxBrk;
  131.     break;
  132.   }
  133.  
  134.   return brokerobj;
  135. }
  136.